package app.utils;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Denny on 2017/6/28.
 * 由于在idea中会报错，重新创建
 */
public class StringUtil {

    private final static String[] hexDigits = {"0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};

    /**
     * 私有构造方法，防止类的实例化，因为工具类不需要实例化。
     */
    private StringUtil() {
    }

    public static String getSuffix(String pathfilename) {
        int i = pathfilename.lastIndexOf(".");
        if (i != -1 && i < pathfilename.length()) {
            return pathfilename.substring(i + 1, pathfilename.length());
        }
        return "";
    }

    public static String getRandomString(int length) {
        Random random = new Random();
        String rstr = "";
        String[] letter = {"A", "B", "C", "D", "E", "F", "G", "H", "J", "K",
                "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y",
                "Z", "2", "3", "4", "5", "6", "7", "8", "9"};

        for (int i = 0; i < length; i++) {
            rstr += letter[random.nextInt(letter.length)];
        }
        return rstr;
    }

    /**
     * 转换字节数组为16进制字串
     *
     * @param b 字节数组
     * @return 16进制字串
     */
    public static String byteArrayToHexString(byte[] b) {
        StringBuffer resultSb = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            resultSb.append(byteToHexString(b[i]));
        }
        return resultSb.toString();
    }

    /**
     * J 转换byte到16进制
     *
     * @param b
     * @return
     */
    public static String byteToHexString(byte b) {
        int n = b;
        if (n < 0) {
            n = 256 + n;
        }
        int d1 = n / 16;
        int d2 = n % 16;
        return hexDigits[d1] + hexDigits[d2];
    }

    public static byte[] hexStringToByteArray(String hex) {
        int len = (hex.length() / 2);
        byte[] result = new byte[len];
        char[] achar = hex.toCharArray();
        for (int i = 0; i < len; i++) {
            int pos = i * 2;
            result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
        }
        return result;
    }

    private static int toByte(char c) {
        byte b = (byte) "0123456789abcdef".indexOf(c);
        return b;
    }

    /**
     * 此方法将给出的字符串source使用delim划分为单词数组。
     *
     * @param source 需要进行划分的原字符串
     * @param delim  单词的分隔字符串
     * @return 划分以后的数组，如果source为null的时候返回以source为唯一元素的数组，
     * 如果delim为null则使用逗号作为分隔字符串。
     * @since 0.1
     */
    public static String[] split(String source, String delim) {
        String[] wordLists;
        if (source == null) {
            wordLists = new String[1];
            wordLists[0] = source;
            return wordLists;
        }
        if (delim == null) {
            delim = ",";
        }
        StringTokenizer st = new StringTokenizer(source, delim);
        int total = st.countTokens();
        wordLists = new String[total];
        for (int i = 0; i < total; i++) {
            wordLists[i] = st.nextToken();
        }
        return wordLists;
    }

    /**
     * 此方法将给出的字符串source使用delim划分为单词数组。
     *
     * @param source 需要进行划分的原字符串
     * @param delim  单词的分隔字符
     * @return 划分以后的数组，如果source为null的时候返回以source为唯一元素的数组。
     * @since 0.2
     */
    public static String[] split(String source, char delim) {
        return split(source, String.valueOf(delim));
    }

    /**
     * 此方法将给出的字符串source使用逗号划分为单词数组。
     *
     * @param source 需要进行划分的原字符串
     * @return 划分以后的数组，如果source为null的时候返回以source为唯一元素的数组。
     * @since 0.1
     */
    public static String[] split(String source) {
        return split(source, ",");
    }

    /**
     * 将字符串中的变量使用values数组中的内容进行替换。 替换的过程是不进行嵌套的，即如果替换的内容中包含变量表达式时不会替换。
     *
     * @param prefix 变量前缀字符串
     * @param source 带参数的原字符串
     * @param values 替换用的字符串数组
     * @return 替换后的字符串。 如果前缀为null则使用“%”作为前缀；
     * 如果source或者values为null或者values的长度为0则返回source；
     * 如果values的长度大于参数的个数，多余的值将被忽略；
     * 如果values的长度小于参数的个数，则后面的所有参数都使用最后一个值进行替换。
     * @since 0.2
     */
    public static String getReplaceString(String prefix, String source,
                                          String[] values) {
        String result = source;
        if (source == null || values == null || values.length < 1) {
            return source;
        }
        if (prefix == null) {
            prefix = "%";
        }

        for (int i = 0; i < values.length; i++) {
            String argument = prefix + Integer.toString(i + 1);
            int index = result.indexOf(argument);
            if (index != -1) {
                String temp = result.substring(0, index);
                if (i < values.length) {
                    temp += values[i];
                } else {
                    temp += values[values.length - 1];
                }
                temp += result.substring(index + 2);
                result = temp;
            }
        }
        return result;
    }

    /**
     * 将字符串中的变量（以“%”为前导后接数字）使用values数组中的内容进行替换。
     * 替换的过程是不进行嵌套的，即如果替换的内容中包含变量表达式时不会替换。
     *
     * @param source 带参数的原字符串
     * @param values 替换用的字符串数组
     * @return 替换后的字符串
     * @since 0.1
     */
    public static String getReplaceString(String source, String[] values) {
        return getReplaceString("%", source, values);
    }

    /**
     * 字符串数组中是否包含指定的字符串。
     *
     * @param strings       字符串数组
     * @param string        字符串
     * @param caseSensitive 是否大小写敏感
     * @return 包含时返回true，否则返回false
     * @since 0.4
     */
    public static boolean contains(String[] strings, String string,
                                   boolean caseSensitive) {
        for (int i = 0; i < strings.length; i++) {
            if (caseSensitive == true) {
                if (strings[i].equals(string)) {
                    return true;
                }
            } else {
                if (strings[i].equalsIgnoreCase(string)) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 字符串数组中是否包含指定的字符串。大小写敏感。
     *
     * @param strings 字符串数组
     * @param string  字符串
     * @return 包含时返回true，否则返回false
     * @since 0.4
     */
    public static boolean contains(String[] strings, String string) {
        return contains(strings, string, true);
    }

    /**
     * 不区分大小写判定字符串数组中是否包含指定的字符串。
     *
     * @param strings 字符串数组
     * @param string  字符串
     * @return 包含时返回true，否则返回false
     * @since 0.4
     */
    public static boolean containsIgnoreCase(String[] strings, String string) {
        return contains(strings, string, false);
    }

    /**
     * 将字符串数组使用指定的分隔符合并成一个字符串。
     *
     * @param array 字符串数组
     * @param delim 分隔符，为null的时候使用""作为分隔符（即没有分隔符）
     * @return 合并后的字符串
     * @since 0.4
     */
    public static String combineStringArray(String[] array, String delim) {
        int length = array.length - 1;
        if (delim == null) {
            delim = "";
        }
        StringBuffer result = new StringBuffer(length * 8);
        for (int i = 0; i < length; i++) {
            result.append(array[i]);
            result.append(delim);
        }
        result.append(array[length]);
        return result.toString();
    }

    public static String combineStringArrayToSqlIn(String[] array) {
        if (array.length > 0) {
            int length = array.length - 1;
            StringBuffer result = new StringBuffer(length * 8);
            for (int i = 0; i < length; i++) {
                result.append("'");
                result.append(array[i]);
                result.append("',");
            }
            result.append("'");
            result.append(array[length]);
            result.append("'");
            return result.toString();
        } else {
            return "";
        }
    }

    public static String combineStringArrayIntToSqlIn(String[] array) {
        if (array.length > 0) {
            int length = array.length - 1;
            StringBuffer result = new StringBuffer(length * 8);
            for (int i = 0; i < length; i++) {
                result.append(array[i]);
                result.append(",");
            }
            result.append(array[length]);
            return result.toString();
        } else {
            return "";
        }
    }

    /**
     * 以指定的字符和长度生成一个该字符的指定长度的字符串。
     *
     * @param c      指定的字符
     * @param length 指定的长度
     * @return 最终生成的字符串
     * @since 0.6
     */
    public static String fillString(char c, int length) {
        String ret = "";
        for (int i = 0; i < length; i++) {
            ret += c;
        }
        return ret;
    }

    /**
     * 去除左边多余的空格。
     *
     * @param value 待去左边空格的字符串
     * @return 去掉左边空格后的字符串
     * @since 0.6
     */
    public static String trimLeft(String value) {
        String result = value;
        if (result == null)
            return result;
        char ch[] = result.toCharArray();
        int index = -1;
        for (int i = 0; i < ch.length; i++) {
            if (Character.isWhitespace(ch[i])) {
                index = i;
            } else {
                break;
            }
        }
        if (index != -1) {
            result = result.substring(index + 1);
        }
        return result;
    }

    /**
     * 去除右边多余的空格。
     *
     * @param value 待去右边空格的字符串
     * @return 去掉右边空格后的字符串
     * @since 0.6
     */
    public static String trimRight(String value) {
        String result = value;
        if (result == null)
            return result;
        char ch[] = result.toCharArray();
        int endIndex = -1;
        for (int i = ch.length - 1; i > -1; i--) {
            if (Character.isWhitespace(ch[i])) {
                endIndex = i;
            } else {
                break;
            }
        }
        if (endIndex != -1) {
            result = result.substring(0, endIndex);
        }
        return result;
    }

    /**
     * 检查密码是否在字符范围A-Z,a-z,0-9,_.@
     *
     * @param str
     * @return boolean
     */
    public static boolean checkPageInput(String str) {
        String regEx = "^[A-Za-z0-9_.@]+$";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.matches();
    }

    /**
     * 检查密码是否在字符范围A-Z,a-z,0-9
     * <p>
     * char and number
     *
     * @param str
     * @return boolean
     */
    public static boolean checkPageInputcn(String str) {
        String regEx = "^[A-Za-z0-9]+$";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.matches();
    }

    /**
     * 检查密码是否在字符范围0-9
     * <p>
     * char and number
     *
     * @param str
     * @return boolean
     */
    public static boolean checkPageInputn(String str) {
        String regEx = "^[0-9]+$";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.matches();
    }

    /**
     * 替换全角字符
     * <p>
     * 替换sql注入的关键字
     *
     * @param strtemp
     * @return
     */
    public static String replaceDoublebyte(String strtemp) {

        if ((strtemp != null) && (strtemp.length() > 0)) {

            strtemp = strtemp.replace("ａ", "a");
            strtemp = strtemp.replace("ｂ", "b");
            strtemp = strtemp.replace("ｃ", "c");
            strtemp = strtemp.replace("ｄ", "d");
            strtemp = strtemp.replace("ｅ", "e");
            strtemp = strtemp.replace("ｆ", "f");
            strtemp = strtemp.replace("ｇ", "g");
            strtemp = strtemp.replace("ｈ", "h");
            strtemp = strtemp.replace("ｉ", "i");
            strtemp = strtemp.replace("ｊ", "j");
            strtemp = strtemp.replace("ｋ", "k");
            strtemp = strtemp.replace("ｌ", "l");
            strtemp = strtemp.replace("ｍ", "m");
            strtemp = strtemp.replace("ｎ", "n");
            strtemp = strtemp.replace("ｏ", "o");
            strtemp = strtemp.replace("ｐ", "p");
            strtemp = strtemp.replace("ｑ", "q");
            strtemp = strtemp.replace("ｒ", "r");
            strtemp = strtemp.replace("ｓ", "s");
            strtemp = strtemp.replace("ｔ", "t");
            strtemp = strtemp.replace("ｕ", "u");
            strtemp = strtemp.replace("ｖ", "v");
            strtemp = strtemp.replace("ｗ", "w");
            strtemp = strtemp.replace("ｘ", "x");
            strtemp = strtemp.replace("ｙ", "y");
            strtemp = strtemp.replace("ｚ", "z");
            strtemp = strtemp.replace("Ａ", "A");
            strtemp = strtemp.replace("Ｂ", "B");
            strtemp = strtemp.replace("Ｃ", "C");
            strtemp = strtemp.replace("Ｄ", "D");
            strtemp = strtemp.replace("Ｅ", "E");
            strtemp = strtemp.replace("Ｆ", "F");
            strtemp = strtemp.replace("Ｇ", "G");
            strtemp = strtemp.replace("Ｈ", "H");
            strtemp = strtemp.replace("Ｉ", "I");
            strtemp = strtemp.replace("Ｊ", "J");
            strtemp = strtemp.replace("Ｋ", "K");
            strtemp = strtemp.replace("Ｌ", "L");
            strtemp = strtemp.replace("Ｍ", "M");
            strtemp = strtemp.replace("Ｎ", "N");
            strtemp = strtemp.replace("Ｏ", "O");
            strtemp = strtemp.replace("Ｐ", "P");
            strtemp = strtemp.replace("Ｑ", "Q");
            strtemp = strtemp.replace("Ｒ", "R");
            strtemp = strtemp.replace("Ｓ", "S");
            strtemp = strtemp.replace("Ｔ", "T");
            strtemp = strtemp.replace("Ｕ", "U");
            strtemp = strtemp.replace("Ｖ", "V");
            strtemp = strtemp.replace("Ｗ", "W");
            strtemp = strtemp.replace("Ｘ", "X");
            strtemp = strtemp.replace("Ｙ", "Y");
            strtemp = strtemp.replace("Ｚ", "Z");
            strtemp = strtemp.replace("０", "0");
            strtemp = strtemp.replace("１", "1");
            strtemp = strtemp.replace("２", "2");
            strtemp = strtemp.replace("３", "3");
            strtemp = strtemp.replace("４", "4");
            strtemp = strtemp.replace("５", "5");
            strtemp = strtemp.replace("６", "6");
            strtemp = strtemp.replace("７", "7");
            strtemp = strtemp.replace("８", "8");
            strtemp = strtemp.replace("９", "9");
            strtemp = strtemp.replace("＝", "=");
            strtemp = strtemp.replace("％", "%");
            strtemp = strtemp.replace("（", "(");
            strtemp = strtemp.replace("）", ")");
            strtemp = strtemp.replace("‘", "'");
            strtemp = strtemp.replace("’", "'");
            strtemp = strtemp.replace("＠", "@");
        }
        return strtemp.toString();
    }

    /**
     * 替换半角字符
     * <p>
     * 替换sql注入的关键字
     *
     * @param strtemp
     * @return
     */
    public static String replaceSinglebyte(String strtemp) {

        if ((strtemp != null) && (strtemp.length() > 0)) {
            strtemp = strtemp.replace("a", "ａ");
            strtemp = strtemp.replace("b", "ｂ");
            strtemp = strtemp.replace("c", "ｃ");
            strtemp = strtemp.replace("d", "ｄ");
            strtemp = strtemp.replace("e", "ｅ");
            strtemp = strtemp.replace("f", "ｆ");
            strtemp = strtemp.replace("g", "ｇ");
            strtemp = strtemp.replace("h", "ｈ");
            strtemp = strtemp.replace("i", "ｉ");
            strtemp = strtemp.replace("j", "ｊ");
            strtemp = strtemp.replace("k", "ｋ");
            strtemp = strtemp.replace("l", "ｌ");
            strtemp = strtemp.replace("m", "ｍ");
            strtemp = strtemp.replace("n", "ｎ");
            strtemp = strtemp.replace("o", "ｏ");
            strtemp = strtemp.replace("p", "ｐ");
            strtemp = strtemp.replace("q", "ｑ");
            strtemp = strtemp.replace("r", "ｒ");
            strtemp = strtemp.replace("s", "ｓ");
            strtemp = strtemp.replace("t", "ｔ");
            strtemp = strtemp.replace("u", "ｕ");
            strtemp = strtemp.replace("v", "ｖ");
            strtemp = strtemp.replace("w", "ｗ");
            strtemp = strtemp.replace("x", "ｘ");
            strtemp = strtemp.replace("y", "ｙ");
            strtemp = strtemp.replace("z", "ｚ");
            strtemp = strtemp.replace("A", "Ａ");
            strtemp = strtemp.replace("B", "Ｂ");
            strtemp = strtemp.replace("C", "Ｃ");
            strtemp = strtemp.replace("D", "Ｄ");
            strtemp = strtemp.replace("E", "Ｅ");
            strtemp = strtemp.replace("F", "Ｆ");
            strtemp = strtemp.replace("G", "Ｇ");
            strtemp = strtemp.replace("H", "Ｈ");
            strtemp = strtemp.replace("I", "Ｉ");
            strtemp = strtemp.replace("J", "Ｊ");
            strtemp = strtemp.replace("K", "Ｋ");
            strtemp = strtemp.replace("L", "Ｌ");
            strtemp = strtemp.replace("M", "Ｍ");
            strtemp = strtemp.replace("N", "Ｎ");
            strtemp = strtemp.replace("O", "Ｏ");
            strtemp = strtemp.replace("P", "Ｐ");
            strtemp = strtemp.replace("Q", "Ｑ");
            strtemp = strtemp.replace("R", "Ｒ");
            strtemp = strtemp.replace("S", "Ｓ");
            strtemp = strtemp.replace("T", "Ｔ");
            strtemp = strtemp.replace("U", "Ｕ");
            strtemp = strtemp.replace("V", "Ｖ");
            strtemp = strtemp.replace("W", "Ｗ");
            strtemp = strtemp.replace("X", "Ｘ");
            strtemp = strtemp.replace("Y", "Ｙ");
            strtemp = strtemp.replace("Z", "Ｚ");
            strtemp = strtemp.replace("0", "０");
            strtemp = strtemp.replace("1", "１");
            strtemp = strtemp.replace("2", "２");
            strtemp = strtemp.replace("3", "３");
            strtemp = strtemp.replace("4", "４");
            strtemp = strtemp.replace("5", "５");
            strtemp = strtemp.replace("6", "６");
            strtemp = strtemp.replace("7", "７");
            strtemp = strtemp.replace("8", "８");
            strtemp = strtemp.replace("9", "９");
            strtemp = strtemp.replace("=", "＝");
            strtemp = strtemp.replace("%", "％");
            strtemp = strtemp.replace("(", "（");
            strtemp = strtemp.replace(")", "）");
            strtemp = strtemp.replace("'", "‘");
            strtemp = strtemp.replace("@", "＠");
        }
        return strtemp.toString();
    }

    public static String replaceSqlChar(String strtemp) {

        if ((strtemp != null) && (strtemp.length() > 0)) {
            strtemp = strtemp.replace("=", "＝");
            strtemp = strtemp.replace("%", "％");
            strtemp = strtemp.replace("(", "（");
            strtemp = strtemp.replace(")", "）");
            strtemp = strtemp.replace("'", "‘");
            strtemp = strtemp.replace("@", "＠");
            strtemp = strtemp.replace("?", "？");
            strtemp = strtemp.replace("\"", "&quot;");
        }
        return strtemp.toString();
    }

    /***
     * 限定为数字
     *
     * @param strtemp
     * @return
     */
    public static String restrictToNumber(String strtemp) {
        StringBuilder ns = new StringBuilder();
        if ((strtemp != null) && (strtemp.length() > 0)) {

            strtemp = strtemp.replace("０", "0");
            strtemp = strtemp.replace("１", "1");
            strtemp = strtemp.replace("２", "2");
            strtemp = strtemp.replace("３", "3");
            strtemp = strtemp.replace("４", "4");
            strtemp = strtemp.replace("５", "5");
            strtemp = strtemp.replace("６", "6");
            strtemp = strtemp.replace("７", "7");
            strtemp = strtemp.replace("８", "8");
            strtemp = strtemp.replace("９", "9");
            for (int i = 0; i < strtemp.length(); i++) {
                char j = strtemp.charAt(i);
                if (j >= '0' && j <= '9') {
                    // 符合要求
                    // System.out.println("StringUtil.onlyNumber()j="+j);
                    ns.append(j);
                } else {
                    // 不符合要求，跳过
                }
            }
        }
        return ns.toString();
    }

    /***
     * 限定为身份证数字+x
     *
     * @param strtemp
     * @return
     */
    public static String restrictToId(String strtemp) {
        StringBuilder ns = new StringBuilder();
        if ((strtemp != null) && (strtemp.length() > 0)) {

            strtemp = strtemp.replace("０", "0");
            strtemp = strtemp.replace("１", "1");
            strtemp = strtemp.replace("２", "2");
            strtemp = strtemp.replace("３", "3");
            strtemp = strtemp.replace("４", "4");
            strtemp = strtemp.replace("５", "5");
            strtemp = strtemp.replace("６", "6");
            strtemp = strtemp.replace("７", "7");
            strtemp = strtemp.replace("８", "8");
            strtemp = strtemp.replace("９", "9");
            strtemp = strtemp.replace("x", "X");// 小写半角
            strtemp = strtemp.replace("ｘ", "X");// 小写全角
            strtemp = strtemp.replace("Ｘ", "X");// 大写全角
            for (int i = 0; i < strtemp.length(); i++) {
                char j = strtemp.charAt(i);
                if ((j >= '0' && j <= '9') || (j == 'X')) {
                    // 符合要求
                    ns.append(j);
                } else {
                    // 不符合要求，跳过
                }
            }
        }
        return ns.toString();
    }

    /***
     * 限定为英文字母加数字
     *
     * @param strtemp
     * @return
     */
    public static String restrictToAZaz09(String strtemp) {

        StringBuilder ns = new StringBuilder();
        if ((strtemp != null) && (strtemp.length() > 0)) {

            strtemp = strtemp.replace("ａ", "a");
            strtemp = strtemp.replace("ｂ", "b");
            strtemp = strtemp.replace("ｃ", "c");
            strtemp = strtemp.replace("ｄ", "d");
            strtemp = strtemp.replace("ｅ", "e");
            strtemp = strtemp.replace("ｆ", "f");
            strtemp = strtemp.replace("ｇ", "g");
            strtemp = strtemp.replace("ｈ", "h");
            strtemp = strtemp.replace("ｉ", "i");
            strtemp = strtemp.replace("ｊ", "j");
            strtemp = strtemp.replace("ｋ", "k");
            strtemp = strtemp.replace("ｌ", "l");
            strtemp = strtemp.replace("ｍ", "m");
            strtemp = strtemp.replace("ｎ", "n");
            strtemp = strtemp.replace("ｏ", "o");
            strtemp = strtemp.replace("ｐ", "p");
            strtemp = strtemp.replace("ｑ", "q");
            strtemp = strtemp.replace("ｒ", "r");
            strtemp = strtemp.replace("ｓ", "s");
            strtemp = strtemp.replace("ｔ", "t");
            strtemp = strtemp.replace("ｕ", "u");
            strtemp = strtemp.replace("ｖ", "v");
            strtemp = strtemp.replace("ｗ", "w");
            strtemp = strtemp.replace("ｘ", "x");
            strtemp = strtemp.replace("ｙ", "y");
            strtemp = strtemp.replace("ｚ", "z");
            strtemp = strtemp.replace("Ａ", "A");
            strtemp = strtemp.replace("Ｂ", "B");
            strtemp = strtemp.replace("Ｃ", "C");
            strtemp = strtemp.replace("Ｄ", "D");
            strtemp = strtemp.replace("Ｅ", "E");
            strtemp = strtemp.replace("Ｆ", "F");
            strtemp = strtemp.replace("Ｇ", "G");
            strtemp = strtemp.replace("Ｈ", "H");
            strtemp = strtemp.replace("Ｉ", "I");
            strtemp = strtemp.replace("Ｊ", "J");
            strtemp = strtemp.replace("Ｋ", "K");
            strtemp = strtemp.replace("Ｌ", "L");
            strtemp = strtemp.replace("Ｍ", "M");
            strtemp = strtemp.replace("Ｎ", "N");
            strtemp = strtemp.replace("Ｏ", "O");
            strtemp = strtemp.replace("Ｐ", "P");
            strtemp = strtemp.replace("Ｑ", "Q");
            strtemp = strtemp.replace("Ｒ", "R");
            strtemp = strtemp.replace("Ｓ", "S");
            strtemp = strtemp.replace("Ｔ", "T");
            strtemp = strtemp.replace("Ｕ", "U");
            strtemp = strtemp.replace("Ｖ", "V");
            strtemp = strtemp.replace("Ｗ", "W");
            strtemp = strtemp.replace("Ｘ", "X");
            strtemp = strtemp.replace("Ｙ", "Y");
            strtemp = strtemp.replace("Ｚ", "Z");
            strtemp = strtemp.replace("０", "0");
            strtemp = strtemp.replace("１", "1");
            strtemp = strtemp.replace("２", "2");
            strtemp = strtemp.replace("３", "3");
            strtemp = strtemp.replace("４", "4");
            strtemp = strtemp.replace("５", "5");
            strtemp = strtemp.replace("６", "6");
            strtemp = strtemp.replace("７", "7");
            strtemp = strtemp.replace("８", "8");
            strtemp = strtemp.replace("９", "9");

            for (int i = 0; i < strtemp.length(); i++) {
                // delete
                char j = strtemp.charAt(i);
                if ((j >= 'A' && j <= 'Z') || (j >= 'a' && j <= 'z')
                        || (j >= '0' && j <= '9') || (j == '_')) {
                    // 符合要求
                    ns.append(j);
                } else {
                    // 不符合要求，跳过
                }
            }

        }
        return ns.toString();
    }

    /***
     * 转换换行符为html中的换行
     *
     * @param str
     * @return
     */
    public static String ControlCharToHtmlTag(String str) {
        char[] cha = {'\n'};
        String enterchar = new String(cha);
        return str.replaceAll(enterchar, "<br/>");
    }

    /**
     * 替换html中的标记为实体
     *
     * @param str
     * @return
     */
    public static String htmlToEntity(String str) {
        str = str.replaceAll("&", "&amp;");
        str = str.replaceAll("<", "&lt;");
        str = str.replaceAll(">", "&gt;");
        str = str.replaceAll("\"", "&quot;");
        str = str.replaceAll("'", "&#39;");
        str = str.replaceAll(" ", "&nbsp;");
        return str;
    }

    /**
     * 还原字符串中的html实体
     *
     * @param str
     * @return
     */
    public static String entityToHtml(String str) {
        str = str.replaceAll("&amp;", "&");
        str = str.replaceAll("&lt;", "<");
        str = str.replaceAll("&gt;", ">");
        str = str.replaceAll("&quot;", "\"");
        str = str.replaceAll("&#39;", "'");
        str = str.replaceAll("&nbsp;", " ");
        return str;
    }

    /**
     * 查找字符串中，指定字符的出现位置
     *
     * @param bigString
     * @param flag
     * @return
     */
    public static Integer[] countFlagInString(String bigString, String flag) {
        List<Integer> flags = new ArrayList<Integer>();
        int i = 0;
        while (i != -1) {
            i = bigString.indexOf(flag, i + flag.length());
            // System.out.println("BArticleDisplay.doGet()，找到位置" + i);
            flags.add(i);
        }
        flags.set(flags.size() - 1, bigString.length());
        return (Integer[]) flags.toArray(new Integer[flags.size()]);

    }

    /**
     * 返回第一字母大写的形式
     *
     * @param source
     * @return
     */
    public static String upperFirstLetter(String source) {
        if (source.length() > 1) {
            return source.substring(0, 1).toUpperCase()
                    + source.substring(1, source.length());
        } else {
            return source.toUpperCase();
        }
    }

    public static String typeMysql2Java(String typeName) {

        if (typeName.contains("(")) {
            typeName = typeName.substring(0, typeName.indexOf("("));
        }
        switch (typeName) {
            case "bigint":
                return "int";
            case "smallint":
                return "int";
            case "int":
                return "int";
            case "char":
                return "String";
            case "varchar":
                return "String";
            case "text":
                return "String";
            case "mediumtext":
                return "String";
            case "tinyint":
                return "boolean";
            case "datetime":
                return "Date";
            case "date":
                return "Date";
            case "timestamp":
                return "Date";
            case "decimal":
                return "float";
            case "numeric":
                return "float";
            default:
                return typeName;
        }
    }

    public static String toDataTypeForMysql4J(String typeName) {

        if (typeName.contains("(")) {
            typeName = typeName.substring(0, typeName.indexOf("("));
        }
        switch (typeName) {
            case "bigint":
                return "int";
            case "smallint":
                return "int";
            case "int":
                return "int";
            case "char":
                return "String";
            case "varchar":
                return "String";
            case "text":
                return "String";
            case "mediumtext":
                return "String";
            case "tinyint":
                return "boolean";
            case "datetime":
                return "Date";
            case "date":
                return "Date";
            case "timestamp":
                return "Date";
            case "decimal":
                return "float";
            case "numeric":
                return "float";
            default:
                return typeName;
        }
    }

    public static String toJavaSeter(String dataType, String fieldName) {
        String rvt = "";
        switch (dataType) {
            case "String":
            case "mediumtext":
                rvt = String.format("titem.set%s(list2[i++].toString());",
                        fieldName);
                break;
            case "int":
                rvt = String.format(
                        "titem.set%s(Integer.parseInt(list2[i++].toString()));",
                        fieldName);
                break;
            case "boolean":
                rvt = String
                        .format("titem.set%s(Boolean.parseBoolean(list2[i++].toString()));",
                                fieldName);
                break;
            case "float":
                rvt = String.format(
                        "titem.set%s(Float.parseFloat(list2[i++].toString()));",
                        fieldName);
                break;
            case "Date":
                rvt = String.format("titem.set%s((%s)list2[i++]);", fieldName,
                        dataType);
                break;
            default:
                rvt = String.format("titem.set%s((%s)list2[i++]);", fieldName,
                        dataType);
                break;
        }
        return rvt;
    }

    public static Date afternDay(Date timepoint, int nDay) {
        // 自动结束置顶的日期（两天后）
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(timepoint);
        calendar.add(Calendar.DATE, nDay);// 把日期往后增加一天.整数往后推,负数往前移动
        Date date = calendar.getTime(); // 这个时间就是日期往后推一天的结果
        return date;
    }
}
